// 云函数入口文件
const cloud = require('wx-server-sdk')

cloud.init({
  env: cloud.DYNAMIC_CURRENT_ENV
})
const db = cloud.database();
const _ = db.command;
const $ = db.command.aggregate;

// 卡密判定
exports.main = async (event, context) => {
  let card_number = event.card_number;
  let card_password = event.card_password;
  console.log('card_number:'+card_number+';card_password:'+card_password);
  let date = new Date();
  let data = await db.collection('ops_card').aggregate().match({
    card_number: card_number,
    card_password: card_password,
    status: 2,
    expiration_date: _.gte(date)
  }).lookup({
    from: 'ops_goods',
    localField: 'goods_id',
    foreignField: '_id',
    as: 'goodsList',
  }).replaceRoot({
    newRoot: $.mergeObjects([$.arrayElemAt(['$goodsList', 0]), '$$ROOT'])
  }).project({
    goodsList: 0,
    qrcode_url: 0,
    card_number: 0,
    card_password: 0,
    Issuer_openid:0,
    card_url: 0
  }).end();
  return data;
}